package com.yanwq.sayhi;

import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.MotionEvent;

import com.yanwq.sayhi.base.BaseActivity;
import com.yanwq.sayhi.common.event.ArticleEvent;
import com.yanwq.sayhi.common.event.UserEvent;
import com.yanwq.sayhi.controller.article.fragment.ArticleListFragment;
import com.yanwq.sayhi.controller.club.fragment.ClubListFragment;
import com.yanwq.sayhi.controller.user.fragment.UserFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import de.greenrobot.event.EventBus;

public class MainActivity extends BaseActivity {

    @BindView(R.id.main_vp)
    ViewPager mVP;

    private ArticleListFragment mALFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        EventBus.getDefault().register(this);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            getWindow().setStatusBarColor(getResources().getColor(R.color.black1_60p));
        }

        hideStatusBar(false);
    }

    @Override
    public void init() {

    }

    @Override
    public void initView() {
        final List<Fragment> list = new ArrayList<>();
        list.add(new UserFragment());
        list.add(mALFragment = new ArticleListFragment());
        list.add(new ClubListFragment());
        MFragmentPagerAdapter adapter = new MFragmentPagerAdapter(getSupportFragmentManager(), list);
        mVP.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        mVP.setAdapter(adapter);
        mVP.setCurrentItem(1);
        mVP.setOffscreenPageLimit(3);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @SuppressWarnings("unused")
    public void onEvent(UserEvent event) {
        switch (event.getEventType()) {
            case UserEvent.LOGOUT:
                finish();
                break;
        }
    }

    @SuppressWarnings("unused")
    public void onEvent(ArticleEvent event) {
        switch (event.getEventType()) {
            case ArticleEvent.MODE_CHANGE:
                mVP.setCurrentItem(1, true);
                break;
        }
    }

    private class MFragmentPagerAdapter extends FragmentPagerAdapter {
        private List<Fragment> mList;

        public MFragmentPagerAdapter(FragmentManager fm, List<Fragment> list) {
            super(fm);
            mList = list;
        }

        @Override
        public Fragment getItem(int position) {
            return mList.get(position);
        }

        @Override
        public int getCount() {
            return mList.size();
        }
    }
}
